package com.jason.fingerprint.common;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.io.IOException;

/**
 * @ClassName: SQLiteDatabaseHelper
 * @Description: 数据库管理类
 * @Author: zhangyujin1989ok@gmail.com
 * @Date: 2015年11月18日16:40:43
 *
 */
public class SQLiteDatabaseHelper {

    private static final String TAG = SQLiteDatabaseHelper.class.getSimpleName();

    private static SQLiteDatabaseHelper sqLiteDatabaseHelper;
    private Context mContent;
    private SQLiteDatabase sqLiteDatabase;
    private String sqlPath;
    private String db_name;
    private String table_name;


    public synchronized static SQLiteDatabaseHelper getInstance(Context context){
        if (sqLiteDatabaseHelper == null) {
            sqLiteDatabaseHelper = new SQLiteDatabaseHelper(context);
        }
        return sqLiteDatabaseHelper;
    }

    private SQLiteDatabaseHelper(Context context){
        mContent = context;
    }

    /***
     *
     * @param sqlPath SD卡中sql文件的路径
     * @param dbName  数据库名字
     * @return SQLiteDatabase
     */
    public SQLiteDatabase openSQLiteDatabase(String sqlPath,String dbName){
        this.sqlPath = sqlPath;
        this.db_name = dbName;
        sqLiteDatabase = createDbFileOnSDCard(sqlPath,dbName);
        return sqLiteDatabase;
    }

    /***
     *
     * @param tableName 表名字
     * @param name      所要判断的字段
     * @return          true: 存在，false:不存在
     */
    public boolean isExistColumnByName(String tableName,String name){
        Cursor cursor = sqLiteDatabase.query("sqlite_master", null, " tbl_name = ? ", new String[]{tableName}, null, null, null);
        if(cursor != null && cursor.moveToFirst()){
            String sql = cursor.getString(cursor.getColumnIndex("sql"));
            Log.i(TAG, " isExistColumnByName sql:" + sql);
            if (!TextUtils.isEmpty(sql) && sql.contains(tableName) && sql.contains(name)){
                return true;
            }
        }
        return false;
    }

    private SQLiteDatabase createDbFileOnSDCard(String sdcardPath, String dbfilename) {
        File dbf = new File(sdcardPath, dbfilename);
        if(!dbf.exists()) {
            try {
                return dbf.createNewFile()?SQLiteDatabase.openOrCreateDatabase(dbf, (SQLiteDatabase.CursorFactory)null):null;
            } catch (IOException var5) {
                throw new RuntimeException("数据库文件创建失败", var5);
            }
        } else {
            return SQLiteDatabase.openOrCreateDatabase(dbf, (SQLiteDatabase.CursorFactory)null);
        }
    }


    public void addColumn(String tableName,String name){
        try{
            boolean flag = isExistColumnByName(tableName,name);
            Log.i(TAG,"isExistColumnByName:" + flag);
            if (!flag){
                sqLiteDatabase.execSQL("ALTER TABLE " + tableName + " ADD COLUMN " + name);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
